//
//  XPlanTableViewCell.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XPlanTableViewCell.h"
#import "XPlanModel.h"

@interface XPlanTableViewCell (){
    
    UILabel* _titleLb;
    UILabel* _nameLb;//名称
    UILabel* _descLb;
    UILabel* _countLb;//数量
    UILabel* _stateLb;//状态
}
/// 显示内容视图
@property(nonatomic,weak) MyLinearLayout* contentLayout;

@end

@implementation XPlanTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = PX(5);
        
        [self initSubViews];
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 10;
    frame.size.width -= 2 * 10;
    [super setFrame:frame];
}

- (void)initSubViews{
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    contentLayout.padding = UIEdgeInsetsMake(16, 16,16, 16);
    contentLayout.myMargin = 0;
    contentLayout.subviewHSpace = 10.0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    contentLayout.cacheEstimatedRect = YES;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    //
    UILabel* titleLb = [UILabel lableWithFont:FONT_FIT(20) color:COLOR_THEME_A text:@"的"];
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.myLeft = titleLb.myCenterY = 0;
    titleLb.mySize = CGSizeMake(33, 33);
    titleLb.layer.cornerRadius = 33.0/2.0f;
    titleLb.layer.masksToBounds = YES;
    titleLb.layer.borderColor = COLOR_THEME_A.CGColor;
    titleLb.layer.borderWidth = 2.0f;
    [contentLayout addSubview:titleLb];
    _titleLb = titleLb;
    

    MyLinearLayout *descLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    descLayout.padding = UIEdgeInsetsMake(0, 0,0, 0);
    descLayout.myMargin = 0;
    descLayout.weight = 1.0;
    descLayout.myHeight = MyLayoutSize.wrap;
//    descLayout.gravity = MyGravity_Vert_Between;
    [contentLayout addSubview:descLayout];
    
    
    UILabel* nameLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_BLACK_DARK text:@"--"];
    nameLb.myHorzMargin = 0;
    [descLayout addSubview:nameLb];
    _nameLb = nameLb;
    
    UILabel* descLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"--"];
    descLb.numberOfLines = 2;
    descLb.myHorzMargin = 0;
    descLb.myTop = 12;
    descLb.myHeight = MyLayoutSize.wrap;
    [descLayout addSubview:descLb];
    _descLb = descLb;
    
    
    UILabel* countLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"0/0 累计:0/0"];
    countLb.myHorzMargin = 0;
    countLb.myTop = 20;
    [descLayout addSubview:countLb];
    _countLb = countLb;
    
    UILabel* statueLb = [UILabel lableWithFont:FONT_FIT(10) color:COLOR_RED text:@"已完成"];
    statueLb.textAlignment = NSTextAlignmentCenter;
    statueLb.myRight = statueLb.myCenterY = 0;
    statueLb.mySize = CGSizeMake(50, 16);
    statueLb.layer.borderColor = COLOR_RED.CGColor;
    statueLb.layer.borderWidth = 1.0f;
    [contentLayout addSubview:statueLb];
    _stateLb = statueLb;
    _stateLb.transform = CGAffineTransformMakeRotation(-M_PI/6);
}


- (void)setModel:(XPlanModel *)model{
    
    _titleLb.text = [model.name substringToIndex:1];
    [_titleLb sizeToFit];
    
    _nameLb.text = model.name;
    [_nameLb sizeToFit];
    
    NSInteger planCount = model.planCount;
    NSInteger todayCount = 0;
    NSInteger totalCount = 0;
    
    RLMResults* clocks = [XPlanDate objectsWhere:@"planId = %ld and time = %@",model.id,[[NSDate date] stringYearMonthDay]];
    if(clocks.count){
        XPlanDate* clock = clocks.firstObject;
        todayCount = clock.count;
    }
    
    RLMResults* totalclocks = [XPlanDate objectsWhere:@"planId = %ld",model.id];
    
    for (XPlanDate* clock in totalclocks) {
        todayCount += clock.count;
    }
    
    _countLb.text = [NSString stringWithFormat:@"%ld/%ld 累计:%ld",todayCount,planCount,totalCount];
    
    _descLb.text = model.desc;
    [_descLb sizeToFit];
    
    _stateLb.visibility = todayCount >= planCount?MyVisibility_Visible:MyVisibility_Gone;
    [_stateLb sizeToFit];
}

#pragma private -- Size

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
    
    CGSize cellSize = CGSizeZero;
    if (@available(iOS 11.0, *)) {
        cellSize = [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width - self.safeAreaInsets.left - self.safeAreaInsets.right - 2*10, targetSize.height)];
    } else {
        CGSize size = targetSize;
        size.width = size.width - 2*10;
        cellSize = [self.contentLayout sizeThatFits:size];  //如果使用系统自带的分割线，请记得将返回的高度height+1
    }
    return cellSize;
}
@end
